<template>
	<view>
		<uni-list-item :title="item.name" 
		v-for="(item,index) in list"
		:key="index" @click="handleEvent(item)">
			<view class="flex align-center text-right text-light-muted" 
			slot="right">
				{{item.data}}
			</view>
		</uni-list-item>
	</view>
</template>

<script>
	import uniListItem from '@/components/uni-ui/uni-list-item/uni-list-item.vue';
	import { mapState } from 'vuex'
	import {getUserOrther} from "@/network/login.js"
	export default {
		components: {
			uniListItem
		},
		data() {
			return {
				list:[]
			}
		},
		computed: {
			...mapState({
				user:state=>state.userInfo
			})
		},
		onShow() {
			this.__init()
		},
		methods: {
			__init(){
				let list = [{
					name:"手机号",
					data:this.user.phone ? this.user.phone : "未绑定",
					type:"navigateTo",
					url:"/pages/user-phone/user-phone"
				},{ 
					name:"登录密码",
					data:this.user.password ? "修改密码" : "未设置",
					type:"navigateTo",
					url:this.user.password ? "/pages/user-password/user-password" : "/pages/set-password/set-password"
				},{ 
					name:"邮箱绑定",
					data:this.user.email ? this.user.email : "未绑定",
					type:"navigateTo",
					url:"/pages/user-email/user-email"
				}]
				this.list = [...list]
				let other = [{
					name:"微信账号",
					data:this.user.wx ? "已绑定" : "未绑定",
					type:"bind",
					provider:"weixin",
					status:1
				},{
					name:"微博账号",
					data:this.user.wb ? "已绑定" : "未绑定",
					type:"bind",
					provider:"sinaweibo",
					status:3
				},{
					name:"QQ账号",
					data:this.user.qq ? "已绑定" : "未绑定",
					type:"bind",
					provider:"qq",
					status:2
				}]
				this.list = [...this.list,...other]	
					
					
			},
			handleEvent(item){
				if(item.type === '') return
				switch (item.type){
					case 'navigateTo':
					uni.navigateTo({
						url: item.url,
					});
						break;
					case 'bind':
					if(item.data !== '未绑定'){
						return uni.showToast({
							title: '你已经绑定过了',
							icon: 'none'
						});
					}
					this.bind(item)
						break;
				}
			},
			// 绑定第三方登录
			bind(item){
				uni.login({
					provider: item.provider,
					success: r => {
						uni.getUserInfo({
							provider: item.provider,
							success:(res)=> {
								let obj = {
									user_id:this.user.id,
									openid:res.userInfo.openId,
									nickname:res.userInfo.nickName,
									avatar:res.userInfo.avatarUrl,
									status:item.status
								}
								console.log(obj)
								getUserOrther(obj).then(attr=>{
									let [err,res] = attr
									console.log(res)
									if(res.data.status){
										let list = {...res.data.data,...res.data.user_other}
										this.$store.commit('initUserInfo',list)
										this.$store.dispatch("openSocket")
										uni.navigateBack({
											delta:1
										})
									}
								})
							}
						});
					},
				});
				
			}
		}
	}
</script>

<style>

</style>
